import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddGitHubAuthFields1703000000002 implements MigrationInterface {
  name = 'AddGitHubAuthFields1703000000002';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Add GitHub ID field to users table
    await queryRunner.query(`
      ALTER TABLE \`users\` 
      ADD COLUMN \`githubId\` varchar(255) NULL UNIQUE
    `);

    // Update provider enum to include 'github'
    await queryRunner.query(`
      ALTER TABLE \`users\` 
      MODIFY COLUMN \`provider\` enum('local', 'google', 'github') NOT NULL DEFAULT 'local'
    `);

    // Add index for GitHub ID
    await queryRunner.query(`
      CREATE INDEX \`IDX_githubId\` ON \`users\` (\`githubId\`)
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Drop GitHub ID index
    await queryRunner.query(`DROP INDEX \`IDX_githubId\` ON \`users\``);

    // Remove GitHub ID field
    await queryRunner.query(`
      ALTER TABLE \`users\` 
      DROP COLUMN \`githubId\`
    `);

    // Revert provider enum to original values
    await queryRunner.query(`
      ALTER TABLE \`users\` 
      MODIFY COLUMN \`provider\` enum('local', 'google') NOT NULL DEFAULT 'local'
    `);
  }
}